home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / textutl3.lha / textutils-1.3 / src / cmp.c < prev    next >
C/C++ Source or Header  |  1992-06-29  |  13KB  |  531 lines

  1. /* cmp -- compare two files.
  2.    Copyright (C) 1988, 1990, 1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Differences from the Unix cmp:
  19.    * 6 - 40 - oo times faster.
  20.    * The file name `-' is always the standard input. If one file name
  21.      is omitted, the standard input is used as well.
  22.    * -c option to print the differing characters like `cat -t'
  23.      (except that newlines are printed as `^J'), with or without -l.
  24.  
  25.    By Torbjorn Granlund and David MacKenzie. */
  26.  
  27. #include <stdio.h>
  28. #include <getopt.h>
  29. #include <sys/types.h>
  30. #include "system.h"
  31.  
  32. #define max(h, i)    ((h) > (i) ? (h) : (i))
  33. #define min(l, o)    ((l) < (o) ? (l) : (o))
  34.  
  35. char *xmalloc ();
  36. int bcmp_cnt ();
  37. int bcmp2 ();
  38. int bread ();
  39. int cmp ();
  40. void printc ();
  41. void error ();
  42.  
  43. /* Name under which this program was invoked.  */
  44.  
  45. char *program_name;
  46.  
  47. /* Filenames of the compared files.  */
  48.  
  49. char *file1;
  50. char *file2;
  51.  
  52. /* File descriptors of the files.  */
  53.  
  54. int file1_desc;
  55. int file2_desc;
  56.  
  57. /* Read buffers for the files.  */
  58.  
  59. char *buf1;
  60. char *buf2;
  61.  
  62. /* Optimal block size for the files.  */
  63.  
  64. int buf_size;
  65.  
  66. /* Output format:
  67.    type_first_diff
  68.      to print the offset and line number of the first differing bytes
  69.    type_all_diffs
  70.      to print the (decimal) offsets and (octal) values of all differing bytes
  71.    type_status
  72.      to only return an exit status indicating whether the files differ */
  73.  
  74. enum
  75.   {
  76.     type_first_diff, type_all_diffs, type_status
  77.   } comparison_type = type_first_diff;
  78.  
  79. /* If nonzero, print values of bytes quoted like cat -t does. */
  80. int opt_print_chars = 0;
  81.  
  82. struct option long_options[] =
  83. {
  84.   {"show-chars", 0, &opt_print_chars, 1},
  85.   {"silent", 0, NULL, 's'},
  86.   {"quiet", 0, NULL, 's'},
  87.   {"verbose", 0, NULL, 'l'},
  88.   {NULL, 0, NULL, 0}
  89. };
  90.  
  91. void
  92. usage (reason)
  93.      char *reason;
  94. {
  95.   if (reason != NULL)
  96.     fprintf (stderr, "%s: %s\n", program_name, reason);
  97.  
  98.   fprintf (stderr, "\
  99. Usage: %s [-cls] [--show-chars] [--verbose] [--silent] [--quiet]\n\
  100.        file1 [file2]\n",
  101.        program_name);
  102.  
  103.   exit (2);
  104. }
  105.  
  106. void
  107. main (argc, argv)
  108.      int argc;
  109.      char *argv[];
  110. {
  111.   int c, opened = 0, exit_status;
  112.   struct stat stat_buf1, stat_buf2;
  113.  
  114.   program_name = argv[0];
  115.  
  116.   /* If an argument is omitted, default to the standard input.  */
  117.  
  118.   file1 = "-";
  119.   file2 = "-";
  120.   file1_desc = 0;
  121.   file2_desc = 0;
  122.  
  123.   /* Parse command line options.  */
  124.  
  125.   while ((c = getopt_long (argc, argv, "cls", long_options, (int *) 0)) != EOF)
  126.     switch (c)
  127.       {
  128.       case 0:
  129.     break;
  130.  
  131.       case 'c':
  132.     opt_print_chars = 1;
  133.     break;
  134.  
  135.       case 'l':
  136.     comparison_type = type_all_diffs;
  137.     break;
  138.  
  139.       case 's':
  140.     comparison_type = type_status;
  141.     break;
  142.  
  143.       default:
  144.     usage ((char *) 0);
  145.       }
  146.  
  147.   if (optind < argc)
  148.     file1 = argv[optind++];
  149.  
  150.   if (optind < argc)
  151.     file2 = argv[optind++];
  152.  
  153.   if (optind < argc)
  154.     usage ("extra arguments");
  155.  
  156.   if (strcmp (file1, "-"))
  157.     {
  158.       opened = 1;
  159.       file1_desc = open (file1, O_RDONLY);
  160.       if (file1_desc < 0)
  161.     {
  162.       if (comparison_type == type_status)
  163.         exit (2);
  164.       else
  165.         error (2, errno, "%s", file1);
  166.     }
  167.     }
  168.  
  169.   if (strcmp (file2, "-"))
  170.     {
  171.       opened = 1;
  172.       file2_desc = open (file2, O_RDONLY);
  173.       if (file2_desc < 0)
  174.     {
  175.       if (comparison_type == type_status)
  176.         exit (2);
  177.       else
  178.         error (2, errno, "%s", file2);
  179.     }
  180.     }
  181.  
  182.   if (file1_desc == file2_desc)
  183.     {
  184.       if (opened)
  185.     error (2, 0, "standard input is closed");
  186.       else
  187.     usage ("at least one filename should be specified");
  188.     }
  189.  
  190.   if (fstat (file1_desc, &stat_buf1) < 0)
  191.     error (2, errno, "%s", file1);
  192.   if (fstat (file2_desc, &stat_buf2) < 0)
  193.     error (2, errno, "%s", file2);
  194.  
  195.   /* If both the input descriptors are associated with plain files,
  196.      we can make the job simpler in some cases.  */
  197.  
  198.   if (S_ISREG (stat_buf1.st_mode) && S_ISREG (stat_buf2.st_mode))
  199.     {
  200.       /* Find out if the files are links to the same inode, and therefore
  201.      identical.  */
  202.  
  203.       if (stat_buf1.st_dev == stat_buf2.st_dev
  204.       && stat_buf1.st_ino == stat_buf2.st_ino)
  205.     exit (0);
  206.  
  207.       /* If output is redirected to "/dev/null", we may assume `-s'.  */
  208.  
  209.       if (comparison_type != type_status)
  210.     {
  211.       struct stat sb;
  212.       dev_t nulldev;
  213.       ino_t nullino;
  214.  
  215.       if (stat ("/dev/null", &sb) == 0)
  216.         {
  217.           nulldev = sb.st_dev;
  218.           nullino = sb.st_ino;
  219.           if (fstat (1, &sb) == 0
  220.           && sb.st_dev == nulldev && sb.st_ino == nullino)
  221.         comparison_type = type_status;
  222.         }
  223.     }
  224.  
  225.       /* If only a return code is needed, conclude that
  226.      the files differ if they have different sizes.  */
  227.  
  228.       if (comparison_type == type_status
  229.       && stat_buf1.st_size != stat_buf2.st_size)
  230.     exit (1);
  231.     }
  232.  
  233.   /* Get the optimal block size of the files.  */
  234.  
  235.   buf_size = max (ST_BLKSIZE (stat_buf1), ST_BLKSIZE (stat_buf2));
  236.  
  237.   /* Allocate buffers, with space for sentinels at the end.  */
  238.  
  239.   buf1 = xmalloc (buf_size + sizeof (long));
  240.   buf2 = xmalloc (buf_size + sizeof (long));
  241.  
  242.   exit_status = cmp ();
  243.  
  244.   if (close (file1_desc) < 0)
  245.     error (2, errno, "%s", file1);
  246.   if (close (file2_desc) < 0)
  247.     error (2, errno, "%s", file2);
  248.   if (comparison_type != type_status
  249.       && (ferror (stdout) || fclose (stdout) == EOF))
  250.     error (2, errno, "write error");
  251.   exit (exit_status);
  252. }
  253.  
  254. /* Compare the two files already open on `file1_desc' and `file2_desc',
  255.    using `buf1' and `buf2'.
  256.    Return 0 if identical, 1 if different, >1 if error. */
  257.  
  258. int
  259. cmp ()
  260. {
  261.   long line_number = 1;        /* Line number (1...) of first difference. */
  262.   long char_number = 1;        /* Offset (1...) in files of 1st difference. */
  263.   int read1, read2;        /* Number of bytes read from each file. */
  264.   int first_diff;        /* Offset (0...) in buffers of 1st diff. */
  265.   int smaller;            /* The lesser of `read1' and `read2'. */
  266.   int ret = 0;
  267.  
  268.   do
  269.     {
  270.       read1 = bread (file1_desc, buf1, buf_size);
  271.       if (read1 < 0)
  272.     error (2, errno, "%s", file1);
  273.       read2 = bread (file2_desc, buf2, buf_size);
  274.       if (read2 < 0)
  275.     error (2, errno, "%s", file2);
  276.  
  277.       /* Insert sentinels for the block compare.  */
  278.  
  279.       buf1[read1] = ~buf2[read1];
  280.       buf2[read2] = ~buf1[read2];
  281.  
  282.       if (comparison_type == type_first_diff)
  283.     {
  284.       int cnt;
  285.  
  286.       /* If the line number should be written for differing files,
  287.          compare the blocks and count the number of newlines
  288.          simultaneously.  */
  289.       first_diff = bcmp_cnt (&cnt, buf1, buf2, '\n');
  290.       line_number += cnt;
  291.     }
  292.       else
  293.     {
  294.       first_diff = bcmp2 (buf1, buf2);
  295.     }
  296.  
  297.       if (comparison_type != type_all_diffs)
  298.     char_number += first_diff;
  299.       else
  300.     smaller = min (read1, read2);
  301.  
  302.       if (first_diff < read1 && first_diff < read2)
  303.     {
  304.       switch (comparison_type)
  305.         {
  306.         case type_first_diff:
  307.           /* This format is a proposed POSIX standard.  */
  308.           printf ("%s %s differ: char %ld, line %ld",
  309.               file1, file2, char_number, line_number);
  310.           if (opt_print_chars)
  311.         {
  312.           printf (" is");
  313.           printf (" %3o ",
  314.               (unsigned) (unsigned char) buf1[first_diff]);
  315.           printc (stdout, 0,
  316.               (unsigned) (unsigned char) buf1[first_diff]);
  317.           printf (" %3o ",
  318.               (unsigned) (unsigned char) buf2[first_diff]);
  319.           printc (stdout, 0,
  320.               (unsigned) (unsigned char) buf2[first_diff]);
  321.         }
  322.           putchar ('\n');
  323.           /* Fall through. */
  324.         case type_status:
  325.           return 1;
  326.         case type_all_diffs:
  327.           while (first_diff < smaller)
  328.         {
  329.           if (buf1[first_diff] != buf2[first_diff])
  330.             {
  331.               if (opt_print_chars)
  332.             {
  333.               printf ("%6ld", first_diff + char_number);
  334.               printf (" %3o ",
  335.                   (unsigned) (unsigned char) buf1[first_diff]);
  336.               printc (stdout, 4,
  337.                   (unsigned) (unsigned char) buf1[first_diff]);
  338.               printf (" %3o ",
  339.                   (unsigned) (unsigned char) buf2[first_diff]);
  340.               printc (stdout, 0,
  341.                   (unsigned) (unsigned char) buf2[first_diff]);
  342.               putchar ('\n');
  343.             }
  344.               else
  345.             /* This format is a proposed POSIX standard. */
  346.             printf ("%6ld %3o %3o\n",
  347.                 first_diff + char_number,
  348.                 (unsigned) (unsigned char) buf1[first_diff],
  349.                 (unsigned) (unsigned char) buf2[first_diff]);
  350.             }
  351.           first_diff++;
  352.         }
  353.           ret = 1;
  354.           break;
  355.         }
  356.     }
  357.  
  358.       if (comparison_type == type_all_diffs)
  359.     char_number += smaller;
  360.  
  361.       if (read1 != read2)
  362.     {
  363.       switch (comparison_type)
  364.         {
  365.         case type_first_diff:
  366.         case type_all_diffs:
  367.           /* This format is a proposed POSIX standard. */
  368.           fprintf (stderr, "%s: EOF on %s\n",
  369.               program_name, read1 < read2 ? file1 : file2);
  370.           break;
  371.         case type_status:
  372.           break;
  373.         }
  374.       return 1;
  375.     }
  376.     }
  377.   while (read1);
  378.   return ret;
  379. }
  380.  
  381. /* Compare two blocks of memory P1 and P2 until they differ,
  382.    and count the number of occurences of the character C in the common
  383.    part of P1 and P2.
  384.    Assumes that P1 and P2 are aligned at long addresses!
  385.    If the blocks are not guaranteed to be different, put sentinels at the ends
  386.    of the blocks before calling this function.
  387.    Return the offset of the first byte that differs.
  388.    Place the count at the address pointed to by COUNT.  */
  389.  
  390. int
  391. bcmp_cnt (count, p1, p2, c)
  392.      int *count;
  393.      char *p1, *p2;
  394.      unsigned char c;
  395. {
  396.   long w;            /* Word for counting C. */
  397.   long i1, i2;            /* One word from each buffer to compare. */
  398.   long *p1i, *p2i;        /* Pointers into each buffer. */
  399.   char *p1c, *p2c;        /* Pointers for finding exact address. */
  400.   unsigned int cnt = 0;        /* Number of occurrences of C. */
  401.   long cccc;            /* C, four times. */
  402.   long m0, m1, m2, m3;        /* Bitmasks for counting C. */
  403.  
  404.   cccc = ((long) c << 24) | ((long) c << 16) | ((long) c << 8) | ((long) c);
  405.  
  406.   m0 = 0xff;
  407.   m1 = 0xff00;
  408.   m2 = 0xff0000;
  409.   m3 = 0xff000000;
  410.  
  411.   p1i = (long *) p1;
  412.   p2i = (long *) p2;
  413.  
  414.   /* Find the rough position of the first difference by reading long ints,
  415.      not bytes.  */
  416.  
  417.   i1 = *p1i++;
  418.   i2 = *p2i++;
  419.   while (i1 == i2)
  420.     {
  421.       w = i1 ^ cccc;
  422.       cnt += (w & m0) == 0;
  423.       cnt += (w & m1) == 0;
  424.       cnt += (w & m2) == 0;
  425.       cnt += (w & m3) == 0;
  426.       i1 = *p1i++;
  427.       i2 = *p2i++;
  428.     }
  429.  
  430.   /* Find out the exact differing position (endianess independant).  */
  431.  
  432.   p1c = (char *) (p1i - 1);
  433.   p2c = (char *) (p2i - 1);
  434.   while (*p1c == *p2c)
  435.     {
  436.       cnt += c == *p1c;
  437.       p1c++;
  438.       p2c++;
  439.     }
  440.  
  441.   *count = cnt;
  442.   return p1c - p1;
  443. }
  444.  
  445. /* Compare two blocks of memory P1 and P2 until they differ.
  446.    Assumes that P1 and P2 are aligned at long addresses!
  447.    If the blocks are not guaranteed to be different, put sentinels at the ends
  448.    of the blocks before calling this function.
  449.    Return the offset of the first byte that differs.  */
  450.  
  451. int
  452. bcmp2 (p1, p2)
  453.      char *p1, *p2;
  454. {
  455.   long *i1, *i2;
  456.   char *c1, *c2;
  457.  
  458.   /* Find the rough position of the first difference by reading long ints,
  459.      not bytes.  */
  460.  
  461.   for (i1 = (long *) p1, i2 = (long *) p2; *i1++ == *i2++;)
  462.     ;
  463.  
  464.   /* Find out the exact differing position (endianess independant).  */
  465.  
  466.   for (c1 = (char *) (i1 - 1), c2 = (char *) (i2 - 1); *c1 == *c2; c1++, c2++)
  467.     ;
  468.  
  469.   return c1 - p1;
  470. }
  471.  
  472. /* Read NCHARS bytes from descriptor FD into BUF.
  473.    Return the number of characters successfully read.  */
  474.  
  475. int
  476. bread (fd, buf, nchars)
  477.      int fd;
  478.      char *buf;
  479.      int nchars;
  480. {
  481.   char *bp = buf;
  482.   int nread;
  483.  
  484.   for (;;)
  485.     {
  486.       nread = read (fd, bp, nchars);
  487.       if (nread < 0)
  488.     return -1;
  489.       bp += nread;
  490.       if (nread == nchars || nread == 0)
  491.     break;
  492.       nchars -= nread;
  493.     }
  494.   return bp - buf;
  495. }
  496.  
  497. /* Print character C on stream FS, making nonvisible characters
  498.    visible by quoting like cat -t does.
  499.    Pad with spaces on the right to WIDTH characters.  */
  500.  
  501. void
  502. printc (fs, width, c)
  503.      FILE *fs;
  504.      int width;
  505.      unsigned c;
  506. {
  507.   if (c >= 128)
  508.     {
  509.       putc ('M', fs);
  510.       putc ('-', fs);
  511.       c -= 128;
  512.       width -= 2;
  513.     }
  514.   if (c < 32)
  515.     {
  516.       putc ('^', fs);
  517.       c += 64;
  518.       --width;
  519.     }
  520.   else if (c == 127)
  521.     {
  522.       putc ('^', fs);
  523.       c = '?';
  524.       --width;
  525.     }
  526.  
  527.   putc (c, fs);
  528.   while (--width > 0)
  529.     putc (' ', fs);
  530. }
  531.